<?php

class ReviewsController extends \Phalcon\Mvc\Controller
{

    public function UnfollowAction()
    {
        $company_id = $this->request->getPost('company_id');
        $user_id  = $this->session->get('user_id');
        

        $result = array();

        if(CompanyFollowers::deleteFollowers($company_id,$user_id)){
            $result['status'] = true;    
        }
        else
        {
            $result['status'] = false;    
        }
        
        echo json_encode($result);
        $this->view->disable();
    }

    public function followAction()
    {
        $company_id = $this->request->getPost('company_id');
        $user_id  = $this->session->get('user_id');
        $result = array();

        if(CompanyFollowers::insertFollowers($company_id,$user_id)){
            $result['status'] = true;    
        }
        else
        {
            $result['status'] = false;    
        }
        
        echo json_encode($result);
        $this->view->disable();
    }
    public function indexAction()
    {

    	$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Reviews');
		$this->view->setVar('content', 'reviews/index');
		$this->view->setVar('title', 'Company Title');

		$cname = $_GET['cname'];

		$company = ClientInfo::findFirst(array(
				"conditions"=>"name = ?0",
				"bind"=>array(0=>$cname)
			));
		$user_id = $this->session->get('user_id');
        $company_id = $company->id;
		
        //LOGIC HERE WHEN TO COUNT AS VISITORS
        //unset($_SESSION['LAST_VISIT'.$company_id]);
        if(!isset($_SESSION['LAST_VISIT'.$company_id])){
            $_SESSION['LAST_VISIT'.$company_id] = CURR_DATE;
            CompanyVisitors::incCounter($user_id,$company_id);
        }
        else{
            $datetime1 = strtotime(CURR_DATE);
            $datetime2 = strtotime($_SESSION['LAST_VISIT'.$company_id]);
            $secs = $datetime1 - $datetime2;// == <seconds between the two times>
            if($secs >= 2){
                $_SESSION['LAST_VISIT'.$company_id] = CURR_DATE;
                CompanyVisitors::incCounter($user_id,$company_id);
            }
        }
        

        $reviews = CompanyRatings::listReviews($company_id);
        $average_rating = CompanyRatings::getAvg($company_id);
        $is_verified = ($company->status == 1) ? "true" : "false";
        $is_followed = CompanyFollowers::checkIfFollowed($user_id,$company_id);
        $total_followers = CompanyFollowers::listTotalFollowers($company_id);
        $total_reviews = sizeof($reviews);


        $this->view->setVar("total_reviews",$total_reviews);
        $this->view->setVar("average_rating",(!$average_rating)?0:$average_rating);
        $this->view->setVar("company",$company);
        $this->view->setVar("reviews",$reviews);
        $this->view->setVar('is_verified',$is_verified);
        $this->view->setVar('is_followed',$is_followed);
        $this->view->setVar('total_followers',$total_followers);
        $this->view->setVar('total_workers',($company->noofworkers)?$company->noofworkers:0);
        $this->view->setVar('company_id',$company_id);

        


    }

    public function createAction()
    {
    	$data =  array(
            'company_id' => $this->request->getPost('company_id'),
            'user_id' => $this->session->get('user_id'),
            'reviews' => nl2br($this->request->getPost('reviews')),
            'rate' => $this->request->getPost('rate'),
            'fullname' => $this->session->get('fullname'),
            'profile_pic' => $this->session->get('profile_pic')
        );
   
    	$result = array();
    	
        $CHECK = CompanyRatings::isValid($data['user_id'],$data['company_id']);
        if($CHECK)
        {
            $result['status'] = false;
            $result['messages'] = "You are only allowed to rate once in every 3 days to avoid spamming.";
            echo json_encode($result);
            $this->view->disable();
            exit();
        }

        $review = new CompanyRatings();
    	if(!$review->save($data))
    	{
    		$result['status'] = false;
    		echo json_encode($result);
    		$this->view->disable();
    		exit();
    	}

        $data['date_created'] = $review->date_created;
        $data['rate_id'] = $review->id;

        $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
        $pusher->trigger('company-channel', 'added-review', $data);
        
        $result['data'] = $data;
    	$result['status'] = true;
		echo json_encode($result);
		$this->view->disable();
    }
}

